﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    public static class BaseExtensions
    {
        /// <summary>
        /// 二进制转16进制
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string BinaryToHex(this byte[] data)
        {
            if (data == null)
            {
                return null;
            }
            var array = new char[checked(data.Length*2)];
            for (int i = 0; i < data.Length; i++)
            {
                byte b = data[i];
                array[2*i] = NibbleToHex((byte) (b >> 4));
                array[2*i + 1] = NibbleToHex((byte) (b & 15));
            }
            return new string(array);
        }

        private static char NibbleToHex(byte nibble)
        {
            return (char) ((nibble < 10) ? (nibble + 48) : (nibble - 10 + 65));
        }
    }
}